﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Shapes;
using HalconDisplayControl.Attributes;
using HalconDisplayControl.Shape;
using HalconDotNet;
using static HalconDisplayControl.HDisplay;

namespace HalconDisplayControl
{
    public partial class HDisplay : HSmartWindowControl
    {
        public HDisplay()
        {
            InitializeComponent();

            HMouseDown += HDisplay_HMouseDown;
            MouseWheel += HDisplay_HMouseWheel;
        }

        #region 字段
        private IShape nowDrawingShape;
        private HTuple drawId;
        #endregion

        private void HDisplay_HMouseWheel(object sender, MouseEventArgs e)
        {
            Point pt = Location;
            MouseEventArgs newe = new MouseEventArgs(e.Button, e.Clicks, e.X - pt.X, e.Y - pt.Y, e.Delta);
            HSmartWindowControl_MouseWheel(this, newe);
        }


        private void HDisplay_HMouseDown(object sender, HMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (nowDrawingShape != null) DrawOk();
            }
        }

        public void Draw(IShape shape)
        {
            if (nowDrawingShape != null) drawClear();
            nowDrawingShape = shape;
            shape.CreateDrawingObj(out drawId);
            HOperatorSet.AttachDrawingObjectToWindow(HalconWindow, drawId);
        }

        // 绘制结束，设置参数到图形对象
        public void DrawOk()
        {
            // 设置绘制对象的参数
            setDrawingParamValue();
            Drawed?.Invoke(nowDrawingShape);
            drawClear();
        }
        // 绘制结束，不会设置参数到图形对象
        public void DrawCancel()
        {
            drawClear();
        }

        /// <summary>
        /// 绘制完成事件
        /// </summary>
        public event Action<IShape> Drawed;

        private void drawClear()
        {
            // 分离绘制对象
            HOperatorSet.DetachDrawingObjectFromWindow(HalconWindow, drawId);
            HOperatorSet.DetachBackgroundFromWindow(HalconWindow);
            // 清除绘制对象
            drawId = null;
            nowDrawingShape = null;
        }

        /// <summary>
        /// 设置当前绘制图形对象的参数
        /// </summary>
        /// <exception cref="Exception"></exception>
        private void setDrawingParamValue()
        {
            if (nowDrawingShape == null) return;
            HTuple values;
            // 使用反射为循环变量赋值
            Type type = nowDrawingShape.GetType();
            // 循环图形对象属性
            foreach (PropertyInfo t in type.GetProperties())
            {
                // 通过特性获取属性对应的halcon参数名称
                ParamNameAttribute att = t.GetCustomAttribute<ParamNameAttribute>();
                if (att != null)
                {
                    // 获取当前属性的halcon参数值
                    HOperatorSet.GetDrawingObjectParams(drawId, att.ParamName, out values);
                    object value = null;
                    switch (t.PropertyType.Name)
                    {
                        case "Double":
                        case "DOUBLE":
                            value = values.D;
                            break;
                        case "Double[]":
                            value = values.DArr;
                            break;
                        default:
                            throw new Exception("该图形对象的属性类型不支持");
                    }
                    t.SetValue(nowDrawingShape, value);
                }
            }
        }
    }
}
